home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the 3D Game Programming Gurus / gurus.iso / DirectX / dx9sdkcp.exe / SDK (C++) / Bin / DXUtils / Visual Studio 6.0 Wizards / Source Code / Template / d3dapp.h < prev    next >
Encoding:
C/C++ Source or Header  |  2002-11-12  |  6.7 KB  |  146 lines

  1. //-----------------------------------------------------------------------------
  2. // File: D3DApp.h
  3. //
  4. // Desc: Application class for the Direct3D samples framework library.
  5. //-----------------------------------------------------------------------------
  6. #ifndef D3DAPP_H
  7. #define D3DAPP_H
  8.  
  9. //-----------------------------------------------------------------------------
  10. // Error codes
  11. //-----------------------------------------------------------------------------
  12. enum APPMSGTYPE { MSG_NONE, MSGERR_APPMUSTEXIT, MSGWARN_SWITCHEDTOREF };
  13.  
  14. #define D3DAPPERR_NODIRECT3D          0x82000001
  15. #define D3DAPPERR_NOWINDOW            0x82000002
  16. #define D3DAPPERR_NOCOMPATIBLEDEVICES 0x82000003
  17. #define D3DAPPERR_NOWINDOWABLEDEVICES 0x82000004
  18. #define D3DAPPERR_NOHARDWAREDEVICE    0x82000005
  19. #define D3DAPPERR_HALNOTCOMPATIBLE    0x82000006
  20. #define D3DAPPERR_NOWINDOWEDHAL       0x82000007
  21. #define D3DAPPERR_NODESKTOPHAL        0x82000008
  22. #define D3DAPPERR_NOHALTHISMODE       0x82000009
  23. #define D3DAPPERR_NONZEROREFCOUNT     0x8200000a
  24. #define D3DAPPERR_MEDIANOTFOUND       0x8200000b
  25. #define D3DAPPERR_RESETFAILED         0x8200000c
  26. #define D3DAPPERR_NULLREFDEVICE       0x8200000d
  27.  
  28.  
  29.  
  30.  
  31. //-----------------------------------------------------------------------------
  32. // Name: class CD3DApplication
  33. // Desc: A base class for creating sample D3D9 applications. To create a simple
  34. //       Direct3D application, simply derive this class into a class (such as
  35. //       class CMyD3DApplication) and override the following functions, as 
  36. //       needed:
  37. //          OneTimeSceneInit()    - To initialize app data (alloc mem, etc.)
  38. //          InitDeviceObjects()   - To initialize the 3D scene objects
  39. //          FrameMove()           - To animate the scene
  40. //          Render()              - To render the scene
  41. //          DeleteDeviceObjects() - To cleanup the 3D scene objects
  42. //          FinalCleanup()        - To cleanup app data (for exitting the app)
  43. //          MsgProc()             - To handle Windows messages
  44. //-----------------------------------------------------------------------------
  45. class CD3DApplication
  46. {
  47. protected:
  48.     CD3DEnumeration   m_d3dEnumeration;
  49.     CD3DSettings      m_d3dSettings;
  50.  
  51.     // Internal variables for the state of the app
  52.     bool              m_bWindowed;
  53.     bool              m_bActive;
  54.     bool              m_bDeviceLost;
  55.     bool              m_bMinimized;
  56.     bool              m_bMaximized;
  57.     bool              m_bIgnoreSizeChange;
  58.     bool              m_bDeviceObjectsInited;
  59.     bool              m_bDeviceObjectsRestored;
  60.  
  61.     // Internal variables used for timing
  62.     bool              m_bFrameMoving;
  63.     bool              m_bSingleStep;
  64.  
  65.     // Internal error handling function
  66.     HRESULT DisplayErrorMsg( HRESULT hr, DWORD dwType );
  67.  
  68.     // Internal functions to manage and render the 3D scene
  69.     static bool ConfirmDeviceHelper( D3DCAPS9* pCaps, 
  70.         VertexProcessingType vertexProcessingType, D3DFORMAT backBufferFormat );
  71.     void    BuildPresentParamsFromSettings();
  72.     bool    FindBestWindowedMode( bool bRequireHAL, bool bRequireREF );
  73.     bool    FindBestFullscreenMode( bool bRequireHAL, bool bRequireREF );
  74.     HRESULT ChooseInitialD3DSettings();
  75.     HRESULT Initialize3DEnvironment();
  76.     HRESULT HandlePossibleSizeChange();
  77.     HRESULT Reset3DEnvironment();
  78.     HRESULT ToggleFullscreen();
  79.     HRESULT ForceWindowed();
  80.     HRESULT UserSelectNewDevice();
  81.     void    Cleanup3DEnvironment();
  82.     HRESULT Render3DEnvironment();
  83.     virtual HRESULT AdjustWindowForChange();
  84.     virtual void UpdateStats();
  85.  
  86. protected:
  87.     // Main objects used for creating and rendering the 3D scene
  88.     D3DPRESENT_PARAMETERS m_d3dpp;         // Parameters for CreateDevice/Reset
  89.     HWND              m_hWnd;              // The main app window
  90.     HWND              m_hWndFocus;         // The D3D focus window (usually same as m_hWnd)
  91.     HMENU             m_hMenu;             // App menu bar (stored here when fullscreen)
  92.     LPDIRECT3D9       m_pD3D;              // The main D3D object
  93.     LPDIRECT3DDEVICE9 m_pd3dDevice;        // The D3D rendering device
  94.     D3DCAPS9          m_d3dCaps;           // Caps for the device
  95.     D3DSURFACE_DESC   m_d3dsdBackBuffer;   // Surface desc of the backbuffer
  96.     DWORD             m_dwCreateFlags;     // Indicate sw or hw vertex processing
  97.     DWORD             m_dwWindowStyle;     // Saved window style for mode switches
  98.     RECT              m_rcWindowBounds;    // Saved window bounds for mode switches
  99.     RECT              m_rcWindowClient;    // Saved client area size for mode switches
  100.  
  101.     // Variables for timing
  102.     FLOAT             m_fTime;             // Current time in seconds
  103.     FLOAT             m_fElapsedTime;      // Time elapsed since last frame
  104.     FLOAT             m_fFPS;              // Instanteous frame rate
  105.     TCHAR             m_strDeviceStats[90];// String to hold D3D device stats
  106.     TCHAR             m_strFrameStats[90]; // String to hold frame stats
  107.  
  108.     // Overridable variables for the app
  109.     TCHAR*            m_strWindowTitle;    // Title for the app's window
  110.     DWORD             m_dwCreationWidth;   // Width used to create window
  111.     DWORD             m_dwCreationHeight;  // Height used to create window
  112.     bool              m_bShowCursorWhenFullscreen; // Whether to show cursor when fullscreen
  113.     bool              m_bClipCursorWhenFullscreen; // Whether to limit cursor pos when fullscreen
  114.     bool              m_bStartFullscreen;  // Whether to start up the app in fullscreen mode
  115.  
  116.     // Overridable functions for the 3D scene created by the app
  117.     virtual HRESULT ConfirmDevice(D3DCAPS9*,DWORD,D3DFORMAT)   { return S_OK; }
  118.     virtual HRESULT OneTimeSceneInit()                         { return S_OK; }
  119.     virtual HRESULT InitDeviceObjects()                        { return S_OK; }
  120.     virtual HRESULT RestoreDeviceObjects()                     { return S_OK; }
  121.     virtual HRESULT FrameMove()                                { return S_OK; }
  122.     virtual HRESULT Render()                                   { return S_OK; }
  123.     virtual HRESULT InvalidateDeviceObjects()                  { return S_OK; }
  124.     virtual HRESULT DeleteDeviceObjects()                      { return S_OK; }
  125.     virtual HRESULT FinalCleanup()                             { return S_OK; }
  126.  
  127. public:
  128.     // Functions to create, run, pause, and clean up the application
  129.     virtual HRESULT Create( HINSTANCE hInstance );
  130.     virtual INT     Run();
  131.     virtual LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  132.     virtual void    Pause( bool bPause );
  133.     virtual         ~CD3DApplication()                         { }
  134.  
  135.     // Internal constructor
  136.     CD3DApplication();
  137. };
  138.  
  139.  
  140.  
  141.  
  142. #endif
  143.  
  144.  
  145.  
  146.